package expenager.project;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PaymentMethod extends Activity {
	
	Button btnPaymentMethod, btnAddPaymentMethod, btnDeletePaymentMethod;
	int paymentMethodIndex;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.paymentmethod);
		
		btnPaymentMethod = (Button) findViewById(R.id.btnPaymentMethod);
		btnPaymentMethod.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				PaymentMethodDialog();
			}
		});
		
		btnAddPaymentMethod = (Button) findViewById(R.id.btnAddPaymentMethod);
		btnAddPaymentMethod.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				addPaymentMethodDialog();
			}
		});
		
		btnDeletePaymentMethod = (Button) findViewById(R.id.btnDeletePaymentMethod);
		btnDeletePaymentMethod.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				deletePaymentMethodDialog();
			}
		});
		
	}
	
	public void PaymentMethodDialog() {
		final DBAdapter db = new DBAdapter(this);
		final List<String> selectedPaymentMethod = new ArrayList<String>();
		
		db.open();
		
		Cursor c = db.getPaymentMethod();
		final String paymentMethod[] = new String[c.getCount()];
		for(int i=0;i<c.getCount();i++) {
			paymentMethod[i] = c.getString(0).toString();			
			
			if(i!=(c.getCount()-1)) {
				c.moveToNext();
				}
		}
		
		db.close();
		
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		
		dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				if(selectedPaymentMethod.isEmpty()==true){
					Toast.makeText(getApplicationContext(), "Selection Aborted\n\nPlease select at least 1 item", Toast.LENGTH_SHORT).show();
					
				}
				else {
					String arrPaymentMethod[] = new String[selectedPaymentMethod.size()];
					for(int i=0;i<selectedPaymentMethod.size();i++){
						arrPaymentMethod[i] = selectedPaymentMethod.get(i);;
					}
					Toast.makeText(getApplicationContext(), Integer.toString(arrPaymentMethod.length)+" Payment Method type(s) will be displayed on Transactions Page", Toast.LENGTH_SHORT).show();
					db.open();
					db.setPaymentMethodSetting(arrPaymentMethod);
					db.close();
				}
			}
		});
		
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				
			}
		});
		
		dialog.setTitle("Set Payment Method");
		dialog.setMultiChoiceItems(paymentMethod, null, new DialogInterface.OnMultiChoiceClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
				if(arg2==true){
					selectedPaymentMethod.add(paymentMethod[arg1]);
				}
				
				if(arg2==false){
					selectedPaymentMethod.remove(paymentMethod[arg1]);
				}
			}
		});
				
		dialog.show();
	}
	
	public void addPaymentMethodDialog() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("Add");
		dialog.setMessage("Input your new Payment Method:");
		
		final EditText textAdd = new EditText(this);
		textAdd.setInputType(InputType.TYPE_CLASS_TEXT);
		dialog.setView(textAdd);
		
		dialog.setPositiveButton("Add", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				addPaymentMethod(textAdd.getText().toString());
				Toast.makeText(getApplicationContext(), "Added Payment Method: "+textAdd.getText().toString(), Toast.LENGTH_SHORT).show();
			}
		});
		
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});

		dialog.show();	
	}
	
	public void deletePaymentMethodDialog() {
		paymentMethodIndex=0;		
		DBAdapter db = new DBAdapter(this);
		db.open();
		
		Cursor c = db.getPaymentMethod();
		final String paymentMethod[] = new String[c.getCount()];
		for(int i=0;i<c.getCount();i++) {
			paymentMethod[i] = c.getString(0).toString();			
			
			if(i!=(c.getCount()-1)) {
				c.moveToNext();
				}
		}
				
		db.close();
		
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		
		dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				deletePaymentMethod(paymentMethod[paymentMethodIndex]);
				Toast.makeText(getApplicationContext(), "Deleted Payment Method: "+paymentMethod[paymentMethodIndex], Toast.LENGTH_SHORT).show();
			}
		});
		
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				
			}
		});
		
		dialog.setTitle("Delete");
		dialog.setSingleChoiceItems(paymentMethod, 0, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				paymentMethodIndex=arg1;
			}
		});
				
		dialog.show();
	}
	
	public void addPaymentMethod(String addPaymentMethod) {
		DBAdapter db = new DBAdapter(this);
		db.open();
		db.addPaymentMethod(addPaymentMethod);
		db.close();
	}
	
	public void deletePaymentMethod(String deletePaymentMethod) {
		DBAdapter db = new DBAdapter(this);
		db.open();
		db.deletePaymentMethod(deletePaymentMethod);
		db.close();
	}
}
